package com.jrx.anytxn.param.job.cutover.step;

import com.jrx.anytxn.common.uitl.DateUtils;
import com.jrx.anytxn.param.constant.Constant;
import com.jrx.anytxn.param.entity.PrOrganizationTable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.stereotype.Service;

import java.util.Date;
/**
 *   日切任务  - processor
 * @author Qingyao Xing
 * @date 2019/9/9 13:29
 */
@Service
public class CutoverProcessor implements ItemProcessor<PrOrganizationTable, PrOrganizationTable> {
    private static final Logger logger = LoggerFactory.getLogger(CutoverProcessor.class);

    @Override
    public PrOrganizationTable process(PrOrganizationTable organizationTable) {
        PrOrganizationTable organizationModel=new PrOrganizationTable();
        logger.info("执行日切操作业务处理");
        organizationModel.setId(organizationTable.getId());
        //将[当前系统日期] 赋值给[上一处理日期]
        organizationModel.setLastProcessingDate(organizationTable.getToday());
        //将[当前累计日期]赋值给[上一累计日期]
        Date accruThruDate =organizationTable.getAccruThruDate();
        organizationModel.setLastAccruThruDate(accruThruDate);
        //将[下一处理日期]赋值给[当前系统日期]
        Date nextProcessingDate = organizationTable.getNextProcessingDate();
        organizationModel.setToday(nextProcessingDate);
        //跳批天数
        organizationModel.setAccruThruDays(organizationTable.getAccruThruDays());
        Integer accruThruDays =organizationTable.getAccruThruDays();
        if (null!=accruThruDate){
            //将[当前累计日期+跳批天数]赋值给[当前累计日期]
            Date newAccruThruDate = DateUtils.addDay(accruThruDate, accruThruDays);
            organizationModel.setAccruThruDate(newAccruThruDate);
        }
        //将[下一处理日期+跳批天数]赋值给[下一处理日期]
        Date newNextProcessingDate = DateUtils.addDay(nextProcessingDate, accruThruDays);
        organizationModel.setNextProcessingDate(newNextProcessingDate);
        //批处理标志 1
        organizationModel.setBatchInProgressFlag("1");
        //将租户id赋值
        organizationModel.setTenantId(organizationTable.getTenantId());
        organizationModel.setUpdateBy(Constant.DEFAULT_USER);
        return organizationModel;
    }
}
